object Demo05ClassExtend {

  // 多态：父类引用指向子类对象
  def printTwiceId(x: Person): Unit = {
    println(x.getId)
    println(x.getId)
  }

  def main(args: Array[String]): Unit = {
    val tea1: Teacher = new Teacher("001", "张老师", "24", "computer")
    val stu1: Student = new Student("0001", "李四同学", "20", "文科三班")

    println(tea1._tId)
    println(tea1._subject)
    println(tea1._name)
    println(tea1._age)

    println(tea1.getName)
    println(stu1.getName)

    // 调用重写的方法
    println(tea1.getId)
    println(stu1.getId)

    Demo05ClassExtend.printTwiceId(tea1)
    Demo05ClassExtend.printTwiceId(stu1)
  }

}

// 将Teacher、Student类中共有的属性、方法拿出来 放到父类当中
// 子类直接可以通过继承的方式获得父类的属性或者方法 实现代码的复用
class Person(name: String, age: String) {
  val _name: String = name
  var _age: String = age

  def getName: String = {
    return _name
  }

  def getId: String = {
    return "id需要自定定义"
  }
}

// 子类在继承父类时 需要调用父类的构造方法
class Teacher(tId: String, name: String, age: String, subject: String) extends Person(name, age) {
  val _tId: String = tId
  val _subject: String = subject

  // 在子类中 如果父类的方法不符合需求 可以进行 重写
  override def getId: String = {
    return this._tId
  }
}

class Student(sId: String, name: String, age: String, clazz: String) extends Person(name, age) {
  val _sId: String = sId
  val _clazz: String = clazz

  override def getId: String = {
    return this.sId
  }
}
